<template>
  <div class="login-container">
      <div class="login-box">
            <div class="avator">
              <img src="../assets/images/avatar.jpg" alt="头像">
            </div>
            <el-form ref="loginFormRef" label-width="0px" class="login-form" :model="loginForm" :rules="loginRules">
                <!-- 用户名 -->
                <el-form-item prop="username">
                <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
                </el-form-item>
                <!-- 密码-->
                <el-form-item prop="password" >
                <el-input prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" type="password" ></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登陆</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
                <!-- 按钮 -->
            </el-form>
      </div>
  </div>
</template>

<script>
export default {
  name: '',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, message: '请输入名字', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10位', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '请输入5-10位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //   重置按钮
    resetLoginForm () {
      console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        // eslint-disable-next-line no-useless-return
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) {
          this.$message.error('登陆失败')
        } else {
          this.$message.success('登陆成功')
          console.log(res)
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
    .login-container{
        background-color: #2b4b6b;
        height: 100%;
    }
    .login-box{
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-top: -150px;
        margin-left: -225px;
        .avator{
        position: absolute;
        height: 150px;
        width: 150px;
        left: 50%;
        transform: translate3d(-50%,-50%,0);
        top: 0;
        border: 1px #eee solid;
        border-radius: 50%;
        box-shadow:  0px 0px 5px 5px #888888;;
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
            }
    }
}
.login-form{
    padding: 0 10px;
    position: absolute;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
}
.btns{
    display: flex;
    justify-content:  flex-end;
}
</style>
